{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeElement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #移除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组 <code>nums</code><em>&nbsp;</em>和一个值 <code>val</code>，你需要 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">原地</a></strong> 移除所有数值等于&nbsp;<code>val</code><em>&nbsp;</em>的元素，并返回移除后数组的新长度。</p>\n",
    "\n",
    "<p>不要使用额外的数组空间，你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">原地 </a>修改输入数组</strong>。</p>\n",
    "\n",
    "<p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>说明:</strong></p>\n",
    "\n",
    "<p>为什么返回数值是整数，但输出的答案是数组呢?</p>\n",
    "\n",
    "<p>请注意，输入数组是以<strong>「引用」</strong>方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。</p>\n",
    "\n",
    "<p>你可以想象内部操作如下:</p>\n",
    "\n",
    "<pre>\n",
    "// <strong>nums</strong> 是以“引用”方式传递的。也就是说，不对实参作任何拷贝\n",
    "int len = removeElement(nums, val);\n",
    "\n",
    "// 在函数里修改输入数组对于调用者是可见的。\n",
    "// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\n",
    "for (int i = 0; i &lt; len; i++) {\n",
    "&nbsp; &nbsp; print(nums[i]);\n",
    "}\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [3,2,2,3], val = 3\n",
    "<strong>输出：</strong>2, nums = [2,2]\n",
    "<strong>解释：</strong>函数应该返回新的长度 <strong><code>2</code></strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如，函数返回的新长度为 2 ，而 nums = [2,2,3,3] 或 nums = [2,2,0,0]，也会被视作正确答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,1,2,2,3,0,4,2], val = 2\n",
    "<strong>输出：</strong>5, nums = [0,1,3,0,4]\n",
    "<strong>解释：</strong>函数应该返回新的长度 <strong><code>5</code></strong>, 并且 nums 中的前五个元素为 <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>3</code></strong>, <strong><code>0</code></strong>, <strong><code>4</code></strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 50</code></li>\n",
    "\t<li><code>0 &lt;= val &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-element](https://leetcode.cn/problems/remove-element/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-element](https://leetcode.cn/problems/remove-element/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,2,2,3]\\n3', '[0,1,2,2,3,0,4,2]\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        i = 0\n",
    "        for n in nums:\n",
    "            if n != val:\n",
    "                nums[i] = n\n",
    "                i += 1\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#\r\n",
    "# @lc app=leetcode.cn id=27 lang=python3\r\n",
    "#\r\n",
    "# [27] 移除元素\r\n",
    "#\r\n",
    "# https://leetcode-cn.com/problems/remove-element/description/\r\n",
    "#\r\n",
    "# algorithms\r\n",
    "# Easy (53.07%)\r\n",
    "# Total Accepted:    36.5K\r\n",
    "# Total Submissions: 68.8K\r\n",
    "# Testcase Example:  '[3,2,2,3]\\n3'\r\n",
    "#\r\n",
    "# 给定一个数组 nums 和一个值 val，你需要原地移除所有数值等于 val 的元素，返回移除后数组的新长度。\r\n",
    "# \r\n",
    "# 不要使用额外的数组空间，你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。\r\n",
    "# \r\n",
    "# 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。\r\n",
    "# \r\n",
    "# 示例 1:\r\n",
    "# \r\n",
    "# 给定 nums = [3,2,2,3], val = 3,\r\n",
    "# \r\n",
    "# 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。\r\n",
    "# \r\n",
    "# 你不需要考虑数组中超出新长度后面的元素。\r\n",
    "# \r\n",
    "# \r\n",
    "# 示例 2:\r\n",
    "# \r\n",
    "# 给定 nums = [0,1,2,2,3,0,4,2], val = 2,\r\n",
    "# \r\n",
    "# 函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。\r\n",
    "# \r\n",
    "# 注意这五个元素可为任意顺序。\r\n",
    "# \r\n",
    "# 你不需要考虑数组中超出新长度后面的元素。\r\n",
    "# \r\n",
    "# \r\n",
    "# 说明:\r\n",
    "# \r\n",
    "# 为什么返回数值是整数，但输出的答案是数组呢?\r\n",
    "# \r\n",
    "# 请注意，输入数组是以“引用”方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。\r\n",
    "# \r\n",
    "# 你可以想象内部操作如下:\r\n",
    "# \r\n",
    "# // nums 是以“引用”方式传递的。也就是说，不对实参作任何拷贝\r\n",
    "# int len = removeElement(nums, val);\r\n",
    "# \r\n",
    "# // 在函数里修改输入数组对于调用者是可见的。\r\n",
    "# // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。\r\n",
    "# for (int i = 0; i < len; i++) {\r\n",
    "# print(nums[i]);\r\n",
    "# }\r\n",
    "# \r\n",
    "# \r\n",
    "#\r\n",
    "class Solution:\r\n",
    "    def removeElement(self, nums: 'List[int]', val: 'int') -> 'int':\r\n",
    "        i=0\r\n",
    "        j=0\r\n",
    "        if len(nums)==0:\r\n",
    "            return 0\r\n",
    "        for j in range(len(nums)):\r\n",
    "            if nums[j]!=val:\r\n",
    "                nums[i]=nums[j]\r\n",
    "                i+=1\r\n",
    "        return i\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums, val):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :type val: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # i = 0\n",
    "        # while i < len(nums):\n",
    "        #     if nums[i] == val:\n",
    "        #         del nums[i]\n",
    "        #     else:\n",
    "        #         i += 1\n",
    "        # return len(nums)\n",
    "        start, end = 0, len(nums)-1\n",
    "        while(start<=end):\n",
    "            if nums[start]==val:\n",
    "                nums[start],nums[end],end = nums[end],nums[start],end-1\n",
    "            else:\n",
    "                start=start+1\n",
    "        return start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums, val):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :type val: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        p=len(set(nums))\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n:\n",
    "            print(nums)\n",
    "            if nums[i]==val:\n",
    "                nums.pop(i)\n",
    "                n=n-1\n",
    "                i=i\n",
    "                print(nums,n,i)\n",
    "            else:\n",
    "                i=i+1\n",
    "                # print(nums)\n",
    "        print(len(nums))\n",
    "        return len(nums)\n",
    "        # while val in nums:\n",
    "        #     nums.remove(val)\n",
    "        # return p\n",
    "        # while nums.count(val)>0:\n",
    "        #     nums.remove(val)\n",
    "        # return len(nums)\n",
    "    \n",
    "                \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums, val):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :type val: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(nums)==0:return 0\n",
    "        \n",
    "        i=0\n",
    "        \n",
    "        for j in range(0,len(nums)):\n",
    "            if nums[j]!=val:\n",
    "                nums[i]=nums[j]\n",
    "                i+=1\n",
    "        \n",
    "        return i\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self,nums,val):\n",
    "        index = len(nums) - 1\n",
    "\n",
    "        while index >= 0:\n",
    "            if nums[index] == val:\n",
    "                nums.remove(val)\n",
    "            index -= 1\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self,nums,val):\n",
    "        index = len(nums) - 1\n",
    "\n",
    "        while index >= 0:\n",
    "            if nums[index] == val:\n",
    "                nums.remove(val)\n",
    "            index -= 1\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "      x = 0\n",
    "      for i in range(len(nums)):\n",
    "        if nums[i] != val:\n",
    "          nums[x] = nums[i]\n",
    "          print(x,i)\n",
    "          x += 1\n",
    "      return min(x,len(nums))\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef removeElement(self, nums: 'List[int]', val: 'int') -> 'int':\n",
    "\t\ti,j=0,0\n",
    "\t\twhile j<len(nums):\n",
    "\t\t\tif nums[j]!=val:\n",
    "\t\t\t\tnums[i]=nums[j]\n",
    "\t\t\t\ti+=1\n",
    "\t\t\tj+=1\n",
    "\t\treturn i\n",
    "\t\t\t\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "              # i为不同元素的数组的长度\n",
    "        i = 0\n",
    "        c = nums.copy()\n",
    "        for j in range(0, len(nums)):\n",
    "            a = j\n",
    "            # 如果nums[j]不等于val, 则将nums[j]赋值给nums[i]即可, i自增\n",
    "            if nums[j] != val:\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        while val in nums:\n",
    "            nums.remove(val)\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        i = 0\n",
    "        for j in range(len(nums)):\n",
    "            if nums[j] != val:\n",
    "                nums[i]= nums[j]\n",
    "                i+=1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        k = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != val:\n",
    "                nums[k] = nums[i]\n",
    "                k += 1\n",
    "        return k "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        while val in nums:\n",
    "            nums.remove(val)\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        length = len(nums)\n",
    "        i=0\n",
    "        for _ in range(length):\n",
    "            if nums[i] != val:\n",
    "                i += 1\n",
    "            else:\n",
    "                del nums[i]\n",
    "        return i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        idx=0\n",
    "        while idx < len(nums):\n",
    "            if nums[idx]==val:\n",
    "                del nums[idx]\n",
    "            else:\n",
    "                idx+=1\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        j = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != val:\n",
    "                nums[j] = nums[i]\n",
    "                j += 1\n",
    "        return j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        if not nums:\n",
    "            return \n",
    "        slow = 0\n",
    "        fast = 0\n",
    "\n",
    "        while fast<len(nums):\n",
    "            print(fast,nums[fast])\n",
    "            if nums[fast] != val:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow+=1 \n",
    "            fast += 1\n",
    "        return slow \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        i = 0\n",
    "        for index in range(len(nums)):\n",
    "            print (1)\n",
    "            if nums[index]!= val :\n",
    "                nums[i] = nums[index]\n",
    "                i += 1\n",
    "                print (i)\n",
    "        return i\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        if len(nums) == 1:\n",
    "            if nums[0] == val:\n",
    "                nums.remove(nums[0])\n",
    "                return len(nums)\n",
    "            else:\n",
    "                return len(nums)\n",
    "        else:\n",
    "            for i in range(len(nums)-1):\n",
    "                for j in nums:\n",
    "                    print(j)\n",
    "                    if j == val:\n",
    "                        nums.remove(j)\n",
    "                        print(nums)\n",
    "            if len(nums) == 1:\n",
    "                if nums[0] == val:\n",
    "                    nums.remove(nums[0])\n",
    "                    return len(nums)\n",
    "                else:\n",
    "                    return len(nums)       \n",
    "        #print(nums)\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        if(nums==[]):\n",
    "            return 0\n",
    "        i = 0\n",
    "\n",
    "        for j in nums:\n",
    "            if(j!=val):\n",
    "                nums[i] = j\n",
    "                i+=1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        # for num in nums:\n",
    "        while 1:\n",
    "            try:\n",
    "                nums.remove(val)\n",
    "            except:\n",
    "                return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        lengh = len(nums)\n",
    "        i = 0\n",
    "        while i < lengh:\n",
    "            if (nums[i] == val):\n",
    "                j=i+1\n",
    "                while (j<lengh):\n",
    "                    nums[j - 1] = nums[j]\n",
    "                    j= j+ 1\n",
    "                i=i- 1\n",
    "                lengh = lengh -  1\n",
    "            i += 1\n",
    "        return lengh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        if len(nums)==0:\n",
    "            return 0\n",
    "\n",
    "        slow, fast = 0, 0\n",
    "        while fast < len(nums):\n",
    "            if nums[fast] != val:\n",
    "\n",
    "                nums[slow] = nums[fast]\n",
    "                slow +=1  #判断不成立前，已经加1了\n",
    "            fast += 1\n",
    "        return slow\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        # if(len(nums) == 0):\n",
    "        #     return 0;\n",
    "        # if(len(nums) == 1):\n",
    "        #     if nums[0] == val:\n",
    "        #         return 0;\n",
    "        #     else: return 1;\n",
    "        # l = 0;\n",
    "        # r = len(nums) - 1;\n",
    "        # while(l < r):\n",
    "        #     while(nums[l] != val and l < len(nums)):\n",
    "        #         l+=1\n",
    "        #     while(nums[r] == val and r >= 0):\n",
    "        #         r-=1\n",
    "        #     nums[l],nums[r]=nums[r],nums[l]\n",
    "        #     l+=1;\n",
    "        #     r-=1;\n",
    "        # return l+1;\n",
    "        right=len(nums);\n",
    "        left=0;\n",
    "        for i in range(right):\n",
    "            if nums[i]!=val:\n",
    "               nums[left],nums[i]=nums[i],nums[left];\n",
    "               left=left+1;\n",
    "               print(nums[i])\n",
    "        return left;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from logging import exception\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\r\n",
    "        new_index = 0\r\n",
    "        for i in range(len(nums)):\r\n",
    "            if nums[i] == val:\r\n",
    "                i += 1\r\n",
    "            else:\r\n",
    "                if new_index != i:\r\n",
    "                    nums[new_index] = nums[i]\r\n",
    "                new_index += 1\r\n",
    "        return new_index\r\n",
    "\r\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        while val in nums:\n",
    "            nums.remove(val)\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "        index = 0\n",
    "        for n in nums:\n",
    "            if n == val:\n",
    "                continue\n",
    "            nums[index] = n\n",
    "            index += 1\n",
    "        return index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeElement(self, nums: List[int], val: int) -> int:\n",
    "\n",
    "        slow = 0 \n",
    "        fast = 0 \n",
    "        while fast < len(nums):\n",
    "            if nums[fast] != val:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow += 1 \n",
    "            fast += 1 \n",
    "\n",
    "        return slow"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
